package org.example.service.impl;

import org.example.dao.IAddressDao;
import org.example.entity.AddressEntity;
import org.example.service.IAddressService;
import org.example.service.IDistrictService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Coder
 */
@Service
public class AddressServiceImpl implements IAddressService {
    private final IDistrictService districtService;
    private final IAddressDao addressDao;

    public AddressServiceImpl(IDistrictService districtService, IAddressDao addressDao) {
        this.districtService = districtService;
        this.addressDao = addressDao;
    }

    @Override
    public int save(AddressEntity addressEntity) {
        String provinceName = districtService.getName(addressEntity.getProvinceCode().toString());
        String cityName = districtService.getName(addressEntity.getCityCode().toString());
        String areaName = districtService.getName(addressEntity.getAreaCode().toString());
        addressEntity.setProvinceName(provinceName);
        addressEntity.setCityName(cityName);
        addressEntity.setAreaName(areaName);
        return addressDao.insertSelective(addressEntity);
    }

    @Override
    public List<AddressEntity> list(Integer uid) {
        AddressEntity addressEntity = new AddressEntity();
        addressEntity.setUid(uid);
        return addressDao.selectSelective(addressEntity);
    }
}
